/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("sh_progress",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;
using VOL.AppManager.IServices;
using VOL.Core.Enums;
using VOL.Core.Filters;
using VOL.Core.Middleware;
using VOL.Entity.DomainModels;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Controllers
{
    public partial class sh_progressController
    {
        private readonly Ish_progressService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public sh_progressController(
            Ish_progressService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }


        [ActionLog("查询")]
        [ApiActionPermission("sh_progress", ActionPermissionOptions.Search)]
        [HttpPost, Route("GetData")]
        public ActionResult GetData([FromBody] shprogressdto param)
        {
            return JsonNormal(InvokeService("GetData", new object[] { param }));
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="pageRequest">type check</param>
        /// <returns></returns>
        [ActionLog("导出")]
        [ApiActionPermission("sh_progress", ActionPermissionOptions.Export)]
        [Produces("application/octet-stream")]
        [HttpPost, Route("ExportFile")]
        //[ProducesResponseType(200)]
        //[ProducesResponseType(401)]
        public IActionResult ExportExcel([FromBody] shprogressdto param)
        {
            try
            {

                var res = InvokeService("GetData", new object[] { param }) as PageGridData<shprogressdto>;
                var data = res.rows;
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet();
                // 第一行 标题
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("地市名称");
                row.CreateCell(2).SetCellValue("申请次数");
                row.CreateCell(3).SetCellValue("申请时间");
                row.CreateCell(4).SetCellValue("审核状态");
                row.CreateCell(5).SetCellValue("审核时间");
                row.CreateCell(6).SetCellValue("意见反馈时间");
                row.CreateCell(7).SetCellValue("交验次数");
                row.CreateCell(8).SetCellValue("线下提交时间");
                row.CreateCell(9).SetCellValue("提交状态");
                row.CreateCell(10).SetCellValue("验收状态");
                row.CreateCell(11).SetCellValue("开始验收时间");
                row.CreateCell(12).SetCellValue("验收完成时间");
                row.CreateCell(13).SetCellValue("申请人");
                row.CreateCell(14).SetCellValue("单位名称");
                row.CreateCell(15).SetCellValue("审核人");

                // 第二行 写数据
                int i = 1;
                foreach (var item in data)
                {
                    if (item != null)
                    {
                        row = sheet.CreateRow(i);
                        row.CreateCell(0).SetCellValue(i);
                        row.CreateCell(1).SetCellValue(item.citymc);
                        row.CreateCell(2).SetCellValue(item.sqcs);
                        row.CreateCell(3).SetCellValue(item.create_time);
                        row.CreateCell(4).SetCellValue(item.shzt);
                        row.CreateCell(5).SetCellValue(item.sh_time);
                        row.CreateCell(6).SetCellValue(item.yjfkdate);
                        row.CreateCell(7).SetCellValue(item.jycs);
                        row.CreateCell(8).SetCellValue(item.xxtj_time);
                        row.CreateCell(9).SetCellValue(item.xxtj_state);
                        row.CreateCell(10).SetCellValue(item.accept_state);
                        row.CreateCell(11).SetCellValue(item.startdate);
                        row.CreateCell(12).SetCellValue(item.enddate);
                        row.CreateCell(13).SetCellValue(item.UserTrueName);
                        row.CreateCell(14).SetCellValue(item.WorkUnit);
                        row.CreateCell(15).SetCellValue(item.sh_name);

                        i++;
                    }
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", DateTime.Now.ToString("yyyyMMddHHssmm") + ".xlsx");
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        private object InvokeService(string methodName, object[] parameters)
        {
            return Service.GetType().GetMethod(methodName).Invoke(Service, parameters);
        }
    }
}
